package dk.wifiathome;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Application;
import android.location.Location;

public class positionList extends Application
{
	private ArrayList<listData> array_list = null;
	
	wifiathomeFileHandler listFileHandler;
	static String ListFileLocation = "/sdcard/Android/data/dk.wifiathome/files/";
	static String ListFileName = "list.xml";
	
    public positionList()
    {
    	super();
    	array_list = new ArrayList<listData>();
    	
		listFileHandler = new wifiathomeFileHandler(this, ListFileLocation, ListFileName, true);
		try {
			ArrayList<listData> tempArray_list;
			tempArray_list = listFileHandler.readFromXmlListFile();
			if (tempArray_list != null)
				array_list = tempArray_list;
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
        
	public ArrayList<listData> getPositionArrayList() {
		return array_list;
	}
	
	public void AddPositionListEntry(String wifiSpotname, Location NewCellLocationEntry, Location NewGpsLocationEntry)
	{
        listData NewObjectToAdd = new listData();
        NewObjectToAdd.setWifiSpotName(wifiSpotname);
        
        if(NewCellLocationEntry != null)
        {
            NewObjectToAdd.setWifiSpotCellInfo(NewCellLocationEntry.getLatitude(), NewCellLocationEntry.getLongitude());
        }
        if(NewGpsLocationEntry != null)
        {
        	NewObjectToAdd.setWifiSpotGpsInfo(NewGpsLocationEntry.getLatitude(), NewGpsLocationEntry.getLongitude());
        }
       
        listFileHandler.writeListElementToXMLFile(NewObjectToAdd.getWifiSpotName(), NewObjectToAdd.getWifiSpotCellInfoLat(), NewObjectToAdd.getWifiSpotCellInfoLong(), 
        		NewObjectToAdd.getWifiSpotGpsInfoLat(), NewObjectToAdd.getWifiSpotGpsInfoLong());
       
        array_list.add(NewObjectToAdd);        
	}
	
	public void RemovePositionListEntry(int itemNumber)
	{
		array_list.remove(itemNumber);
		listFileHandler.writeXMLFileHeaderToFile();
		for (int i = 0; i < array_list.size(); i++)
		{
	        listData NewObjectToAdd = array_list.get(i);
	        listFileHandler.writeListElementToXMLFile(NewObjectToAdd.getWifiSpotName(), NewObjectToAdd.getWifiSpotCellInfoLat(), NewObjectToAdd.getWifiSpotCellInfoLong(), 
	        		NewObjectToAdd.getWifiSpotGpsInfoLat(), NewObjectToAdd.getWifiSpotGpsInfoLong());
	    }
	}
}